package com.shopping.controller;

import com.alibaba.fastjson.JSONObject;
import com.shopping.constant.ResponseCode;
import com.shopping.pojo.RecMessage;
import com.shopping.service.message.MessageServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class WebController {

    @Autowired
    MessageServiceImpl messageService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PostMapping(value = "/message/connect")
    public Map<String, Object> connect(@RequestBody JSONObject jsonObject){
        Map<String, Object> map = new HashMap<>();
        String uuid = jsonObject.getString("uuid");
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        int myId = redisUserId;
        int userId = jsonObject.getInteger("userId");
        String nickName = jsonObject.getString("nickName");
        String context = jsonObject.getString("context");
        RecMessage recMessage = new RecMessage(myId, userId, nickName);
        return messageService.addMessage(recMessage, context);
    }

    @PostMapping(value = "/message/obliterate")
    public Map<String, Object> Obliterate(@RequestBody JSONObject jsonObject){
        Map<String, Object> map = new HashMap<>();
        String uuid = jsonObject.getString("uuid");
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        int id = jsonObject.getInteger("id");
        return messageService.deleteMessage(id);
    }

    @PostMapping(value = "/message/obliterateAll")
    public Map<String, Object> ObliterateAll(@RequestBody JSONObject jsonObject){
        Map<String, Object> map = new HashMap<>();
        String uuid = jsonObject.getString("uuid");
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        return messageService.deleteAllMessage(redisUserId);
    }

    @PostMapping(value = {"/message/showAll"})
    public Map<String, Object> showAll(@RequestBody JSONObject jsonObject){
        Map<String, Object> map = new HashMap<>();
        String uuid = jsonObject.getString("uuid");
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        return messageService.showMessages(redisUserId);
    }

}
